﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class InventoryLogService : BaseService, IInventoryLogService
    {
        private readonly IInventoryLogRepository _inventoryLogRepository;
        public InventoryLogService(IMapper mapper, IInventoryLogRepository _inventoryLogRepository) : base(mapper)
        {
            this._inventoryLogRepository = _inventoryLogRepository;
        }

        public List<InventoryLog> GetInventoryflow(long WarehouseId, long AreaId, int SourceType, string? MaterialName, int pageIndex, int pageSize, out int totalCount)
        {
            var list = _inventoryLogRepository.GetEntityAlls();
            if (WarehouseId > 0)
            {
                list = list.Where(x => x.WarehouseId == WarehouseId);
            }
            if (AreaId > 0)
            {
                list = list.Where(x => x.AreaId == AreaId);
            }
            if (SourceType < 5)
            {
                list = list.Where(x => x.SourceType == SourceType);
            }
            if (MaterialName != null)
            {
                list = list.Where(x => x.MaterialName.Contains(MaterialName));
            }
            var lists = _inventoryLogRepository.GetEntityAlls(list, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);

            return lists.ToList();
        }

        public List<InventoryLog> GetInventoryMsgDown(int i)
        {
            List<InventoryLog> list = new List<InventoryLog>();
            if (i == 1)
            {
                list = _inventoryLogRepository.GetEntityWhereAll(x => x.SourceType == 0).ToList();
            }
            else if (i == 2)
            {
                list = _inventoryLogRepository.GetEntityWhereAll(x => x.SourceType == 1).ToList();
            }
            return list;
        }

        public List<InventoryLog> InventorySnapshot(DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex, int pageSize)
        {
            totalCount = 0;
            var query = _inventoryLogRepository.GetEntityAll();
            if (startDate != null)
            {
                query = query.Where(t => t.CreateDate >= startDate);
            }
            if (endDate != null)
            {
                query = query.Where(query => query.CreateDate <= endDate);
            }
            query = query.Where(t => t.SourceType == 0);
            totalCount = query.Count();
            var data = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return data;
        }




    }
}
